<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Eclipse,Java基础," />










<meta name="description" content="JRE、JDL、JVM的区别与联系JVM ：英文名称（Java Virtual Machine），就是我们耳熟能详的 Java 虚拟机。它只认识 xxx.class 这种类型的文件，它能够将 class 文件中的字节码指令进行识别并调用操作系统向上的 API 完成动作。所以说，jvm 是 Java 能够跨平台的核心，具体的下文会详细说明。 JRE ：英文名称（Java Runtime Enviro">
<meta property="og:type" content="article">
<meta property="og:title" content="Java基础">
<meta property="og:url" content="http://yangshucheng2020.gitee.io/blog/2020/03/27/Java%E5%9F%BA%E7%A1%80/index.html">
<meta property="og:site_name" content="羊书">
<meta property="og:description" content="JRE、JDL、JVM的区别与联系JVM ：英文名称（Java Virtual Machine），就是我们耳熟能详的 Java 虚拟机。它只认识 xxx.class 这种类型的文件，它能够将 class 文件中的字节码指令进行识别并调用操作系统向上的 API 完成动作。所以说，jvm 是 Java 能够跨平台的核心，具体的下文会详细说明。 JRE ：英文名称（Java Runtime Enviro">
<meta property="og:image" content="http://yangshucheng2020.gitee.io/Users/yangshucheng/Documents/%E6%88%AA%E5%9B%BE/Xnip2020-01-03_16-14-57.jpg">
<meta property="article:published_time" content="2020-03-27T02:59:16.814Z">
<meta property="article:modified_time" content="2020-03-27T04:40:31.556Z">
<meta property="article:author" content="羊书">
<meta property="article:tag" content="Eclipse">
<meta property="article:tag" content="Java基础">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://yangshucheng2020.gitee.io/Users/yangshucheng/Documents/%E6%88%AA%E5%9B%BE/Xnip2020-01-03_16-14-57.jpg">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yangshucheng2020.gitee.io/blog/2020/03/27/Java基础/"/>





  <title>Java基础 | 羊书</title>
  








<meta name="generator" content="Hexo 4.2.0"></head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">羊书</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/blog/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/blog/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yangshucheng2020.gitee.io/blog/blog/2020/03/27/Java%E5%9F%BA%E7%A1%80/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="羊书">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="羊书">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Java基础</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2020-03-27T10:59:16+08:00">
                2020-03-27
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/%E5%AD%A6%E4%B9%A0/" itemprop="url" rel="index">
                    <span itemprop="name">学习</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="JRE、JDL、JVM的区别与联系"><a href="#JRE、JDL、JVM的区别与联系" class="headerlink" title="JRE、JDL、JVM的区别与联系"></a>JRE、JDL、JVM的区别与联系</h2><p>JVM ：英文名称（Java Virtual Machine），就是我们耳熟能详的 Java 虚拟机。它只认识 xxx.class 这种类型的文件，它能够将 class 文件中的字节码指令进行识别并调用操作系统向上的 API 完成动作。所以说，jvm 是 Java 能够跨平台的核心，具体的下文会详细说明。</p>
<p>JRE ：英文名称（Java Runtime Environment），我们叫它：Java 运行时环境。它主要包含两个部分，jvm 的标准实现和 Java 的一些基本类库。它相对于 jvm 来说，多出来的是一部分的 Java 类库。</p>
<p>JDK ：英文名称（Java Development Kit），Java 开发工具包。jdk 是整个 Java 开发的核心，它集成了 jre 和一些好用的小工具。例如：javac.exe，java.exe，jar.exe 等。</p>
<p>显然，这三者的关系是：一层层的嵌套关系。JDK&gt;JRE&gt;JVM</p>
<h2 id="java程序编写-编译-运行的过程"><a href="#java程序编写-编译-运行的过程" class="headerlink" title="java程序编写-编译-运行的过程"></a>java程序编写-编译-运行的过程</h2><p>编写：我们将编写的java代码保存在”.java”结尾的源文件中</p>
<p>编译：使用javac.exe命令编译我们的java源文件。格式：javac 源文件名.java</p>
<p>编译的过程：编译以后，会生成一个或多个字节码文件。字节码文件的文件名与java源文件中的类名相同。</p>
<p>运行：使用java.exe命令解释运行我们的字节码文件。格式：java 类名</p>
<h2 id="java-程序注意"><a href="#java-程序注意" class="headerlink" title="java 程序注意"></a>java 程序注意</h2><p>在一个java源文件中可以声明多个class。但是，只能最多有一个类声明为public</p>
<p>而且要求声明为public的类的类名必须与源文件名相同</p>
<p>程序的入口是main()方法。格式是固定的</p>
<p>每一行执行语句都以”;”结束。</p>
<h2 id="java数据类型"><a href="#java数据类型" class="headerlink" title="java数据类型"></a>java数据类型</h2><p>long型需要在数字后面加L或者l</p>
<p>如：  </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">long</span> a = <span class="number">212312312312124L</span>;</span><br><span class="line"><span class="comment">//或者</span></span><br><span class="line"><span class="keyword">long</span> b = <span class="number">213212412312312l</span>;</span><br></pre></td></tr></table></figure>

<p>float 型 末尾加 “F”或者“f”</p>
<h2 id="输出语句"><a href="#输出语句" class="headerlink" title="输出语句"></a>输出语句</h2><p>System.out.println():先输出数据，然后换行<br>System.out.print():只输出数据</p>
<h2 id="输出数组"><a href="#输出数组" class="headerlink" title="输出数组"></a>输出数组</h2><ul>
<li>调用Array.toString(a)，返回一个包含数组元素的字符串，这些元素被放置在括号内，并用逗号分开</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span>[] arr = &#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>&#125;; </span><br><span class="line">	System.out.println(Arrays.toString(arr));</span><br></pre></td></tr></table></figure>

<p>输出：[1, 2, 3, 4, 5]</p>
<ul>
<li><p>传统的for循环方式</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;array.length;i++)</span><br><span class="line">&#123;</span><br><span class="line">      System.out.println(array[i]);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



</li>
</ul>
<ul>
<li><p>for each循环</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> a:array)</span><br><span class="line">    System.out.println(a);</span><br></pre></td></tr></table></figure>


</li>
</ul>
<h2 id="注释"><a href="#注释" class="headerlink" title="注释"></a>注释</h2><h3 id="单行注释"><a href="#单行注释" class="headerlink" title="单行注释"></a>单行注释</h3><p>//xxxx</p>
<h3 id="多行注释"><a href="#多行注释" class="headerlink" title="多行注释"></a>多行注释</h3><p>/*     …..     */</p>
<h3 id="文档注释"><a href="#文档注释" class="headerlink" title="文档注释"></a>文档注释</h3><p><strong>/**</strong>  ….   */</p>
<p>说明注释允许你在程序中嵌入关于程序的信息。你可以使用 javadoc 工具软件来生成信息，并输出到HTML文件中。</p>
<p>说明注释，使你更加方便的记录你的程序信息。</p>
<p>在开始的 <strong>/**</strong> 之后，第一行或几行是关于类、变量和方法的主要描述。</p>
<p>之后，你可以包含一个或多个各种各样的 <strong>@</strong> 标签。每一个 <strong>@</strong> 标签必须在一个新行的开始或者在一行的开始紧跟星号(*).</p>
<p>多个相同类型的标签应该放成一组。例如，如果你有三个 <strong>@see</strong> 标签，可以将它们一个接一个的放在一起。</p>
<p>@author 作者</p>
<p>@version 版本</p>
<p>例子：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> * &lt;p&gt;项目名称: $&#123;project_name&#125; &lt;/p&gt; </span></span><br><span class="line"><span class="comment"> * &lt;p&gt;文件名称: $&#123;file_name&#125; &lt;/p&gt; </span></span><br><span class="line"><span class="comment"> * &lt;p&gt;描述: [类型描述] &lt;/p&gt;</span></span><br><span class="line"><span class="comment"> * &lt;p&gt;创建时间: $&#123;date&#125; &lt;/p&gt;</span></span><br><span class="line"><span class="comment"> * &lt;p&gt;公司信息: ************公司 *********部&lt;/p&gt; </span></span><br><span class="line"><span class="comment"> * @author &lt;a href="mail to: *******@******.com" rel="nofollow"&gt;作者&lt;/a&gt;</span></span><br><span class="line"><span class="comment"> * @version v1.0</span></span><br><span class="line"><span class="comment"> * @update [序号][日期YYYY-MM-DD] [更改人姓名][变更描述]</span></span><br><span class="line"><span class="comment"> */</span></span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Title</span>：$&#123;enclosing_method&#125;</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span>: [功能描述]</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Param</span>: $&#123;tags&#125;</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Return</span>: $&#123;return_type&#125;</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> &lt;a href="mail to: *******@******.com" rel="nofollow"&gt;作者&lt;/a&gt;</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@CreateDate</span>: $&#123;date&#125; $&#123;time&#125;&lt;/p&gt; </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@update</span>: [序号][日期YYYY-MM-DD] [更改人姓名][变更描述]     </span></span><br><span class="line"><span class="comment"> */</span></span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 获取  $&#123;bare_field_name&#125;</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 设置   $&#123;bare_field_name&#125; </span></span><br><span class="line"><span class="comment"> * ($&#123;param&#125;)$&#123;field&#125;</span></span><br><span class="line"><span class="comment"> */</span></span><br></pre></td></tr></table></figure>

<h2 id="Java-关键字"><a href="#Java-关键字" class="headerlink" title="Java 关键字"></a>Java 关键字</h2><p>定义：被java语言赋予了特殊含义，用作专门用途的字符串（单词）</p>
<p>特点：关键字中所有字母均为小写</p>
<p><img src="/Users/yangshucheng/Documents/%E6%88%AA%E5%9B%BE/Xnip2020-01-03_16-14-57.jpg" alt="Xnip2020-01-03_16-14-57"></p>
<p>![image-20200103161246326](/Users/yangshucheng/Library/Application Support/typora-user-images/image-20200103161246326.png)</p>
<table>
<thead>
<tr>
<th></th>
<th>数据</th>
<th>类型</th>
<th>关键字</th>
<th></th>
</tr>
</thead>
<tbody><tr>
<td>class</td>
<td>interface</td>
<td>enum</td>
<td>byte</td>
<td>short</td>
</tr>
<tr>
<td>int</td>
<td>long</td>
<td>float</td>
<td>double</td>
<td>char</td>
</tr>
<tr>
<td>boolean</td>
<td>void</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td>流程控制</td>
<td>关键字</td>
<td></td>
<td></td>
</tr>
<tr>
<td>if</td>
<td>else</td>
<td>switch</td>
<td>case</td>
<td>default</td>
</tr>
<tr>
<td>while</td>
<td>do</td>
<td>for</td>
<td>break</td>
<td>continue</td>
</tr>
<tr>
<td>return</td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td>访问权限</td>
<td>关键字</td>
<td></td>
<td></td>
</tr>
<tr>
<td>private</td>
<td>protect</td>
<td>public</td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td>类、函数</td>
<td>变量修饰符</td>
<td>的关键字</td>
<td></td>
</tr>
<tr>
<td>abstract</td>
<td>final</td>
<td>static</td>
<td>synchronized</td>
<td></td>
</tr>
<tr>
<td></td>
<td>类与类</td>
<td>之间关系</td>
<td>的关键字</td>
<td></td>
</tr>
<tr>
<td>extends</td>
<td>implements</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td>建立实例</td>
<td>判断实例</td>
<td>的关键字</td>
<td></td>
</tr>
<tr>
<td>new</td>
<td>this</td>
<td>super</td>
<td>instanceof</td>
<td></td>
</tr>
<tr>
<td></td>
<td>异常处理</td>
<td>的关键字</td>
<td></td>
<td></td>
</tr>
<tr>
<td>try</td>
<td>catch</td>
<td>finally</td>
<td>throw</td>
<td>throws</td>
</tr>
<tr>
<td></td>
<td>包</td>
<td>的关键字</td>
<td></td>
<td></td>
</tr>
<tr>
<td>package</td>
<td>import</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td>其他修饰符</td>
<td>的关键字</td>
<td></td>
<td></td>
</tr>
<tr>
<td>native</td>
<td>strictfp</td>
<td>tracnsient</td>
<td>volatile</td>
<td>assert数据</td>
</tr>
<tr>
<td></td>
<td>数据类型值</td>
<td>的字面值</td>
<td></td>
<td></td>
</tr>
<tr>
<td>true</td>
<td>false</td>
<td>null</td>
<td></td>
<td></td>
</tr>
</tbody></table>
<h2 id="Java保留字"><a href="#Java保留字" class="headerlink" title="Java保留字"></a>Java保留字</h2><p>goto</p>
<p>const</p>
<p>自己命名时要避免使用这些保留字</p>
<h2 id="标识符的使用"><a href="#标识符的使用" class="headerlink" title="标识符的使用"></a>标识符的使用</h2><p>定义：凡是自己可以起名字的地方都叫标识符</p>
<p>设计到的结构：包名、类名、接口名、变量名、方法名、常量名</p>
<p>规则：</p>
<ul>
<li>由26个英文字母大小写，0-9 ，_或 $ 组成</li>
<li>数字不可以开头。</li>
<li>不可以使用关键字和保留字，但能包含关键字和保留字。</li>
<li>Java中严格区分大小写，长度无限制。</li>
<li>标识符不能包含空格。</li>
</ul>
<h2 id="Java中的名称命名规范"><a href="#Java中的名称命名规范" class="headerlink" title="Java中的名称命名规范"></a>Java中的名称命名规范</h2><ul>
<li>Java中的名臣命名规范<ul>
<li><strong>包名</strong>：多单词组成时所有字母都小写：xxxyyyzzz</li>
<li><strong>类名、接口名</strong>：多单词组成时，所有单词的数字母大写：XxxYyyZzz</li>
<li><strong>变量名、方法名</strong>：多单词组成时，第一个单词的首字母小写，第二个单词开始每个单词首字母大写：xxxYyyZzz</li>
<li><strong>常量名</strong>：所有字母都大写。多单词时每个单词用下划线：XXX_YYY_ZZZ</li>
</ul>
</li>
<li>注意1：在起名字时，为了提高阅读性，要尽量有意义，见名知意</li>
<li>注意2：java采用Unicode字符集，因此标识符也可以使用汉字，但是不建议使用</li>
</ul>
<h2 id="Java变量的分类"><a href="#Java变量的分类" class="headerlink" title="Java变量的分类"></a>Java变量的分类</h2><h3 id="按数据类型分"><a href="#按数据类型分" class="headerlink" title="按数据类型分"></a>按数据类型分</h3><h4 id="基本数据类型"><a href="#基本数据类型" class="headerlink" title="基本数据类型"></a>基本数据类型</h4><p>![image-20200103155706233](/Users/yangshucheng/Library/Application Support/typora-user-images/image-20200103155706233.png)</p>
<ul>
<li>数值型<ul>
<li>整数类型(byte,short,int,long)</li>
<li>浮点类型(float,double)</li>
</ul>
</li>
<li>字符型(char)</li>
<li>布尔型(boolean)</li>
</ul>
<h4 id="引用数据类型"><a href="#引用数据类型" class="headerlink" title="引用数据类型"></a>引用数据类型</h4><ul>
<li>类(class) 比如：Sting</li>
<li>接口(interface)</li>
<li>数组( [] )</li>
</ul>
<h4 id="详细说明"><a href="#详细说明" class="headerlink" title="详细说明"></a>详细说明</h4><ol>
<li><p>整型：byte(1字节=8bit)\short(2字节)\int(4字节)\long(8字节)</p>
<p>①byte范围：-128~127</p>
<p>②声明long型变量，必须以”l”或者”L”结尾</p>
<p>③通常，定义整型变量时，使用int型</p>
<p>④整型的常量，默认类型是：int型</p>
<p>![image-20200103155828118](/Users/yangshucheng/Library/Application Support/typora-user-images/image-20200103155828118.png)</p>
</li>
<li><p>浮点型：float(4字节)/double(8字节)</p>
<p>①浮点型，表示带小数点的数值</p>
<p>②float表示数值的范围比long还大</p>
<p>③定义float类型变量时，变量要以”f”或者”F”结尾</p>
<p>④通常，定义浮点型变量时，使用double型</p>
<p>⑤浮点型的常量，默认类型为：double</p>
<p>![image-20200103160237992](/Users/yangshucheng/Library/Application Support/typora-user-images/image-20200103160237992.png)</p>
</li>
<li><p>字符型：char（1字符=2字节）</p>
<p>①定义char型变量，通常使用一对’ ‘,内部只能写一个字符</p>
<p>②表示方式：</p>
<p>​    1.声明一个字符</p>
<p>​    2.转义字符</p>
<p>​    3.直接使用Unicode值来表示字符型常量</p>
</li>
</ol>
<p>   ![image-20200103160330788](/Users/yangshucheng/Library/Application Support/typora-user-images/image-20200103160330788.png)</p>
<ol start="4">
<li><p>布尔型：boolean</p>
<p>①只能取两个值之一：true、false</p>
<p>②常常在条件判断、循环结构中使用</p>
</li>
</ol>
<h3 id="按变量的分类-按声明的位置的不同分类"><a href="#按变量的分类-按声明的位置的不同分类" class="headerlink" title="按变量的分类-按声明的位置的不同分类"></a>按变量的分类-按声明的位置的不同分类</h3><ul>
<li><p>在方法体外，类体内声明的变量称作<strong>成员变量</strong></p>
</li>
<li><p>在方法体内部声明的变量称作<strong>局部变量</strong></p>
<p>![image-20200103171603330](/Users/yangshucheng/Library/Application Support/typora-user-images/image-20200103171603330.png)</p>
</li>
</ul>
<h2 id="变量声明"><a href="#变量声明" class="headerlink" title="变量声明"></a>变量声明</h2><ul>
<li><p>声明变量<br>  语法:&lt;数据类型&gt; &lt;变量名称&gt; </p>
<p>​     例如:int var;</p>
</li>
<li><p>变量的赋值<br>  语法:&lt;变量名称&gt; = &lt;值&gt; </p>
<p>​     例如:var = 10;</p>
</li>
<li><p>声明和赋值变量<br>  语法: &lt;数据类型&gt; &lt;变量名&gt; = &lt;初始化值&gt; </p>
<p>​     例如:int var = 10;</p>
</li>
</ul>
<h3 id="使用变量注意"><a href="#使用变量注意" class="headerlink" title="使用变量注意"></a>使用变量注意</h3><ol>
<li><p>Java中每个变量必须先声明，后使用 </p>
</li>
<li><p>使用变量名来访问这块区域的数据 </p>
</li>
<li><p>变量的作用域:其定义所在的一对{ }内 </p>
</li>
<li><p>变量只有在其作用域内才有效 </p>
</li>
<li><p>同一个作用域内，不能定义重名的变量</p>
</li>
</ol>
<h2 id="基本数据类型变量间运算规则"><a href="#基本数据类型变量间运算规则" class="headerlink" title="基本数据类型变量间运算规则"></a>基本数据类型变量间运算规则</h2><ul>
<li><p>涉及到的基本数据类型：除了boolean外的其他7种</p>
</li>
<li><p>自动类型转换（只涉及到7种基本数据类型）</p>
<p>结论：当容量小的数据类型的变量与容量大的数据类型的变量做运算时，结果自动    提升为容量大的数据类型</p>
<p>byte、char、short –&gt; int –&gt; long –&gt;float –&gt; double</p>
<p>特别的：当byte、char、short 三种类型的变量做运算时，结果为int型</p>
<p>说明：此时的容量大小指的是，表示数的范围的大和小。比如float容量要大于long的容量</p>
</li>
<li><p>强制类型转换（只涉及7种基本数据类型）：自动类型提升运算的逆运算</p>
<ol>
<li><p>需要使用强转符：()</p>
</li>
<li><p>注意点：强制类型转换，可能导致精度损失</p>
</li>
</ol>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//判断是否能通过编译</span></span><br><span class="line"><span class="comment">//1)</span></span><br><span class="line"><span class="keyword">short</span> s = <span class="number">5</span>; </span><br><span class="line">s = s-<span class="number">2</span>; <span class="comment">//判断:no。原因：2是int型的，那么java会把s自动提升为int型，那么结果s+2为int型赋给一个short型会出错，会损失精度，改为s+=2就不会了，因为+=是一个运算符号，java会把2当成short型来处理</span></span><br><span class="line"><span class="comment">//2) </span></span><br><span class="line"><span class="keyword">byte</span> b = <span class="number">3</span>;</span><br><span class="line">b = b + <span class="number">4</span>; <span class="comment">//判断:no</span></span><br><span class="line">b = (<span class="keyword">byte</span>)(b+<span class="number">4</span>); <span class="comment">//判断:yes</span></span><br><span class="line"><span class="comment">//3)</span></span><br><span class="line"><span class="keyword">char</span> c = ‘a’;</span><br><span class="line"><span class="keyword">int</span> i = <span class="number">5</span>;</span><br><span class="line"><span class="keyword">float</span> d = .<span class="number">314F</span>; </span><br><span class="line"><span class="keyword">double</span> result = c+i+d;<span class="comment">//判断:yes</span></span><br><span class="line"><span class="comment">//4)</span></span><br><span class="line"><span class="keyword">byte</span> b = <span class="number">5</span>;</span><br><span class="line"><span class="keyword">short</span> s = <span class="number">3</span>;</span><br><span class="line"><span class="keyword">short</span> t = s + b; <span class="comment">//判断:no。原因：s+b自动转换成int型，int型赋值给short型会出错，会损失精度</span></span><br></pre></td></tr></table></figure>



<h2 id="String与8种基本数据类型间的运算"><a href="#String与8种基本数据类型间的运算" class="headerlink" title="String与8种基本数据类型间的运算"></a>String与8种基本数据类型间的运算</h2><ol>
<li>String属于引用数据类型，翻译为：字符串</li>
<li>声明String类型变量时，使用一对双引号” “</li>
<li>当把任何基本数据类型的值和字符串(String)进行连接运算时(+)，基本数据类型的值将自动转化为字符串(String)类型</li>
<li>String可以和8种基本数据类型做运算，且运算只能是连接运算：+</li>
<li>运算的结果仍然是String类型</li>
</ol>
<ul>
<li><p>避免</p>
<p>String s = 123;//编译错误</p>
<p>String s1 = “123”;</p>
<p>int i = (int)s1;//编译错误</p>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line">String s2 = <span class="string">"a"</span>;</span><br><span class="line">String s3 = <span class="string">""</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">//char c = '';//编译不通过,不能为空</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//***********************</span></span><br><span class="line"><span class="keyword">int</span> number = <span class="number">1001</span>;</span><br><span class="line">String numberStr = <span class="string">"学号："</span>;</span><br><span class="line">String info = numberStr + number;<span class="comment">// +：连接运算</span></span><br><span class="line"><span class="keyword">boolean</span> b1 = <span class="keyword">true</span>;</span><br><span class="line">String info1 = info + b1;<span class="comment">// +：连接运算</span></span><br><span class="line">System.out.println(info1);</span><br><span class="line"></span><br><span class="line"><span class="comment">//***********************</span></span><br><span class="line"><span class="comment">//练习1</span></span><br><span class="line"><span class="keyword">char</span> c = <span class="string">'a'</span>;<span class="comment">//97   A:65</span></span><br><span class="line"><span class="keyword">int</span> num = <span class="number">10</span>;</span><br><span class="line">String str = <span class="string">"hello"</span>;</span><br><span class="line">System.out.println(c + num + str);<span class="comment">//107hello</span></span><br><span class="line">System.out.println(c + str + num);<span class="comment">//ahello10</span></span><br><span class="line">System.out.println(c + (num + str));<span class="comment">//a10hello</span></span><br><span class="line">System.out.println((c + num) + str);<span class="comment">//107hello</span></span><br><span class="line">System.out.println(str + num + c);<span class="comment">//hello10a</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//练习2</span></span><br><span class="line"><span class="comment">//*	*</span></span><br><span class="line">System.out.println(<span class="string">"*	*"</span>);<span class="comment">//* *</span></span><br><span class="line">System.out.println(<span class="string">'*'</span> + <span class="string">'\t'</span> + <span class="string">'*'</span>);<span class="comment">//93</span></span><br><span class="line">System.out.println(<span class="string">'*'</span> + <span class="string">"\t"</span> + <span class="string">'*'</span>);<span class="comment">//* *</span></span><br><span class="line">System.out.println(<span class="string">'*'</span> + <span class="string">'\t'</span> + <span class="string">"*"</span>);<span class="comment">//51*</span></span><br><span class="line">System.out.println(<span class="string">'*'</span> + (<span class="string">'\t'</span> + <span class="string">"*"</span>));<span class="comment">//* *</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//***********************</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//String str1 = 123;//编译不通过</span></span><br><span class="line">String str1 = <span class="number">123</span> + <span class="string">""</span>;</span><br><span class="line">System.out.println(str1);<span class="comment">//"123"</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//int num1 = str1;</span></span><br><span class="line"><span class="comment">//int num1 = (int)str1;//"123"</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> num1 = Integer.parseInt(str1);</span><br><span class="line">System.out.println(num1);<span class="comment">//123</span></span><br></pre></td></tr></table></figure>

<h2 id="二进制"><a href="#二进制" class="headerlink" title="二进制"></a>二进制</h2><p>对于整数，有四种表示方式:</p>
<ul>
<li>二进制(binary):0,1 ，满2进1<strong>.以0b或0B开头</strong>。</li>
<li>十进制(decimal):0-9 ，满10进1。</li>
<li>八进制(octal):0-7 ，满8进1. 以<strong>数字0开头</strong>表示。</li>
<li>十六进制(hex):0-9及A-F，满16进1. <strong>以0x或0X开头</strong>表示。此处的A-F不区分大小写。 如:0x21AF +1= 0X21B0</li>
</ul>
<p>计算机以二进制补码方式存储</p>
<ul>
<li><p>进制基本转换</p>
<p>![image-20200103204104582](/Users/yangshucheng/Library/Application Support/typora-user-images/image-20200103204104582.png)</p>
</li>
</ul>
<h2 id="算数运算符"><a href="#算数运算符" class="headerlink" title="算数运算符"></a>算数运算符</h2><h3 id="运算符之一：算术运算符"><a href="#运算符之一：算术运算符" class="headerlink" title="运算符之一：算术运算符"></a>运算符之一：算术运算符</h3><ol>
<li>+   -   *   /   %  (前)++ (后)++ (前)– (后)– +</li>
<li>(前)++ :先自增1，后运算    ++a<br>(后)++ :先运算，后自增1    a++</li>
<li>(前)– :先自减1，后运算    –a<br>(后)– :先运算，后自减1    a–</li>
<li>%:取余运算<br>结果的符号与被模数的符号相同<br>开发中，经常使用%来判断能否被除尽的情况。</li>
</ol>
<h3 id="运算符之二：赋值运算符"><a href="#运算符之二：赋值运算符" class="headerlink" title="运算符之二：赋值运算符"></a>运算符之二：赋值运算符</h3><p>=  +=  -=  *=  /=  %= </p>
<p>【典型代码】</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> j1 = <span class="number">10</span>;</span><br><span class="line"><span class="keyword">int</span> i2,j2;<span class="comment">//连续赋值</span></span><br><span class="line">i2 = j2 = <span class="number">10</span>;</span><br><span class="line"><span class="keyword">int</span> i3 = <span class="number">10</span>,j3 = <span class="number">20</span>;</span><br></pre></td></tr></table></figure>

<p>【特别说明】</p>
<ol>
<li><p>运算的结果不会改变变量本身的数据类型</p>
 <figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//练习1</span></span><br><span class="line"><span class="keyword">int</span> i = <span class="number">1</span>;</span><br><span class="line">i *= <span class="number">0.1</span>;</span><br><span class="line">System.out.println(i);<span class="comment">//0</span></span><br><span class="line">i++;</span><br><span class="line">System.out.println(i);<span class="comment">//1</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>变量的常见处理方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//开发中，如果希望变量实现+2的操作，有几种方法？(前提：int num = 10;)</span></span><br><span class="line"><span class="comment">//方式一：num = num + 2;</span></span><br><span class="line"><span class="comment">//方式二：num += 2; (推荐)	</span></span><br><span class="line"><span class="comment">//开发中，如果希望变量实现+1的操作，有几种方法？(前提：int num = 10;)</span></span><br><span class="line"><span class="comment">//方式一：num = num + 1;</span></span><br><span class="line"><span class="comment">//方式二：num += 1; </span></span><br><span class="line"><span class="comment">//方式三：num++; (推荐)</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>关于赋值符号 = 两边的类型必须一致</p>
</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">short</span> a = <span class="number">1</span>;</span><br><span class="line"><span class="comment">//a = a + 2;  //编译出错，左右两边类型不一致</span></span><br><span class="line">a += <span class="number">2</span>; <span class="comment">//不会改变变量本身的数据类型</span></span><br><span class="line">System.out.println(a);</span><br></pre></td></tr></table></figure>


<p>注意a的类型：a如果是short类型的变量，在和int类型的1进行运算时会自动的将short-&gt;int, 然后执行复制操作符“=”,就会发生类型的错误，左边是short类型，右边是int类型的变量</p>
<h3 id="运算符之三：比较运算符"><a href="#运算符之三：比较运算符" class="headerlink" title="运算符之三：比较运算符"></a>运算符之三：比较运算符</h3><ul>
<li><p>==  !=  &gt;  &lt;  &gt;= &lt;=  instanceof</p>
</li>
<li><p>结论：</p>
<ol>
<li>比较运算符的结果是boolean类型</li>
<li>区分 ==（比较是否相等）  和  = （赋值）</li>
<li>&gt;  &lt;  &gt;=  &lt;=  : 只能使用在数值类型的数据之间</li>
<li>== : 不仅可以使用在数值类型数据之间，还可以使用在其他引用类型变量之间</li>
</ol>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Account acct1 = <span class="keyword">new</span> Account(<span class="number">1000</span>);</span><br><span class="line">Account acct2 = <span class="keyword">new</span> Account(<span class="number">1000</span>);</span><br><span class="line"><span class="keyword">boolean</span> b1 = (acct1 == acct2);<span class="comment">//比较两个Account是否为同一个账户</span></span><br><span class="line"><span class="keyword">boolean</span> b2 = (acct1 != acct2);<span class="comment">//比较两个Account是否为不同账户</span></span><br></pre></td></tr></table></figure>

<h3 id="运算符之四：逻辑运算符"><a href="#运算符之四：逻辑运算符" class="headerlink" title="运算符之四：逻辑运算符"></a>运算符之四：逻辑运算符</h3><ul>
<li>&amp;  &amp;&amp; |  || ! ^</li>
</ul>
<p>![image-20200128212154109](/Users/yangshucheng/Library/Application Support/typora-user-images/image-20200128212154109.png)</p>
<ul>
<li>区分&amp; 与 &amp;&amp;<br>相同点1：&amp; 与  &amp;&amp; 的<strong>运算结果相同</strong><br>相同点2: 当符号左边是true时，二者都会执行符号右边的运算<br>不同点：当符号左边是false时，<strong>&amp;继续执行</strong>符号右边的运算。<strong>&amp;&amp;不再执行符号</strong>右边的运算。<br>开发中，<strong>推荐使用&amp;&amp;</strong></li>
<li>【特别说明】：逻辑运算符操作的都是boolean类型的变量</li>
</ul>
<h3 id="运算符之四：位运算符"><a href="#运算符之四：位运算符" class="headerlink" title="运算符之四：位运算符"></a>运算符之四：位运算符</h3><ul>
<li><p>&lt;&lt;       &gt;&gt;     &lt;&lt;&lt;      &gt;&gt;&gt;   |  &amp;  ^</p>
</li>
<li><p>&lt;&lt;       &gt;&gt;         (有符号移位运算)</p>
</li>
<li><p>&lt;&lt;&lt;      &gt;&gt;&gt;      (无符号移位运算)</p>
</li>
<li><p>位运算符操作的都是整型的数据</p>
</li>
<li><p>&lt;&lt; ：在一定范围内，每向左移1位，相当于 * 2</p>
</li>
</ul>
<p>​      &gt;&gt; :  在一定范围内，每向右移1位，相当于 / 2</p>
<ul>
<li>【面试题】 最高效方式的计算2 * 8 ？  2 &lt;&lt; 3  或 8 &lt;&lt; 1</li>
</ul>
<h3 id="交换两个变量的办法"><a href="#交换两个变量的办法" class="headerlink" title="交换两个变量的办法"></a>交换两个变量的办法</h3><ul>
<li>异或( ^ )与或( | )的不同之处是:当左右都为true时，结果为false</li>
</ul>
<p>k = m ^ n</p>
<p>m = k ^ n = (m ^ n ) ^ n    </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">  <span class="keyword">int</span> num1 = <span class="number">10</span>;</span><br><span class="line"><span class="keyword">int</span> num2 = <span class="number">20</span>;</span><br><span class="line">System.out.println(<span class="string">"num1 = "</span> + num1 + <span class="string">",num2 = "</span> + num2);</span><br><span class="line"></span><br><span class="line"><span class="comment">//方式一：定义临时变量的方式</span></span><br><span class="line"><span class="comment">//推荐的方式</span></span><br><span class="line"><span class="keyword">int</span> temp = num1;</span><br><span class="line">num1 = num2;</span><br><span class="line">num2 = temp;</span><br><span class="line"></span><br><span class="line"><span class="comment">//方式二：好处：不用定义临时变量  </span></span><br><span class="line"><span class="comment">//弊端：① 相加操作可能超出存储范围 ② 有局限性：只能适用于数值类型</span></span><br><span class="line"><span class="comment">//num1 = num1 + num2;</span></span><br><span class="line"><span class="comment">//num2 = num1 - num2;</span></span><br><span class="line"><span class="comment">//num1 = num1 - num2;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//方式三：使用位运算符</span></span><br><span class="line"><span class="comment">//有局限性：只能适用于数值类型</span></span><br><span class="line"><span class="comment">//num1 = num1 ^ num2;</span></span><br><span class="line"><span class="comment">//num2 = num1 ^ num2;</span></span><br><span class="line"><span class="comment">//num1 = num1 ^ num2;</span></span><br><span class="line"></span><br><span class="line">System.out.println(<span class="string">"num1 = "</span> + num1 + <span class="string">",num2 = "</span> + num2);</span><br></pre></td></tr></table></figure>

<h3 id="如何求一个0-255范围内的整数的十六进制值，例如60的十六进制表示形式3C"><a href="#如何求一个0-255范围内的整数的十六进制值，例如60的十六进制表示形式3C" class="headerlink" title="如何求一个0~255范围内的整数的十六进制值，例如60的十六进制表示形式3C"></a>如何求一个0~255范围内的整数的十六进制值，例如60的十六进制表示形式3C</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//方式一：自动实现</span></span><br><span class="line">String str1 = Integer.toBinaryString(<span class="number">60</span>);</span><br><span class="line">String str2 = Integer.toHexString(<span class="number">60</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//方式二：手动实现</span></span><br><span class="line"><span class="keyword">int</span> i1 = <span class="number">60</span>;</span><br><span class="line"><span class="keyword">int</span> i2 = i1&amp;<span class="number">15</span>;</span><br><span class="line">String j = (i2 &gt; <span class="number">9</span>)? (<span class="keyword">char</span>)(i2-<span class="number">10</span> + <span class="string">'A'</span>)+<span class="string">""</span> : i2+<span class="string">""</span>;</span><br><span class="line"><span class="keyword">int</span> temp = i1 &gt;&gt;&gt; <span class="number">4</span>;</span><br><span class="line">i2 = temp &amp; <span class="number">15</span>;</span><br><span class="line">String k = (i2 &gt; <span class="number">9</span>)? (<span class="keyword">char</span>)(i2-<span class="number">10</span> + <span class="string">'A'</span>)+<span class="string">""</span> : i2+<span class="string">""</span>;</span><br><span class="line">System.out.println(k+<span class="string">""</span>+j);</span><br></pre></td></tr></table></figure>

<h3 id="运算符之四：三元运算符"><a href="#运算符之四：三元运算符" class="headerlink" title="运算符之四：三元运算符"></a>运算符之四：三元运算符</h3><ol>
<li><p>结构：<strong>(条件表达式)? 表达式1 : 表达式2</strong></p>
</li>
<li><p>说明<br>① 条件表达式的结果为boolean类型<br>② 根据条件表达式真或假，决定执行表达式1，还是表达式2.<br>  如果表达式为true，则执行表达式1。<br>  如果表达式为false，则执行表达式2。<br>③ 表达式1 和表达式2要求是一致的。<br>④ 三元运算符可以嵌套使用</p>
</li>
<li><p>凡是可以使用三元运算符的地方，都可以改写为if-else<br>反之，不成立。</p>
</li>
<li><p>如果程序既可以使用三元运算符，又可以使用if-else结构，那么<strong>优先选择三元运算符</strong>。原因：简洁、执行效率高。</p>
</li>
</ol>
<h2 id="流程控制"><a href="#流程控制" class="headerlink" title="流程控制"></a>流程控制</h2><h3 id="分支结构中的if-else（条件判断结构）"><a href="#分支结构中的if-else（条件判断结构）" class="headerlink" title="分支结构中的if-else（条件判断结构）"></a>分支结构中的if-else（条件判断结构）</h3><p>三种结构：</p>
<p>第一种：<br>if(条件表达式){<br>    执行表达式<br>}</p>
<p>第二种：二选一<br>if(条件表达式){<br>    执行表达式1<br>}else{<br>    执行表达式2<br>}</p>
<p>第三种：n选一<br>if(条件表达式){<br>    执行表达式1<br>}else if(条件表达式){<br>    执行表达式2<br>}else if(条件表达式){<br>    执行表达式3<br>}<br>…<br>else{<br>    执行表达式n<br>}</p>
<h3 id="switch-case结构"><a href="#switch-case结构" class="headerlink" title="switch-case结构"></a>switch-case结构</h3><h3 id="循环结构"><a href="#循环结构" class="headerlink" title="循环结构"></a>循环结构</h3><h4 id="for"><a href="#for" class="headerlink" title="for"></a>for</h4><h4 id="while"><a href="#while" class="headerlink" title="while"></a>while</h4><p><strong>While 循环的使用</strong></p>
<p>一、循环结构的4个要素<br>① 初始化条件<br>② 循环条件  —&gt;是boolean类型<br>③ 循环体<br>④ 迭代条件</p>
<p>二、while循环的结构</p>
<p>①<br>while(②){<br>    ③;<br>    ④;<br>}</p>
<p>执行过程：① - ② - ③ - ④ - ② - ③ - ④ - … - ②</p>
<p>说明：<br>1.写while循环千万小心不要丢了迭代条件。一旦丢了，就可能导致死循环！<br>2.我们写程序，要避免出现死循环。<br>3.for循环和while循环是可以相互转换的！<br>  区别：for循环和while循环的初始化条件部分的作用范围不同。</p>
<p>算法：有限性。</p>
<h4 id="do-while"><a href="#do-while" class="headerlink" title="do-while"></a>do-while</h4><p><strong>do-while循环的使用</strong></p>
<p>一、循环结构的4个要素<br>① 初始化条件<br>② 循环条件  —&gt;是boolean类型<br>③ 循环体<br>④ 迭代条件</p>
<p>二、do-while循环结构：</p>
<p>①<br>do{<br>    ③;<br>    ④;</p>
<p>}while(②);</p>
<p>执行过程：① - ③ - ④ - ② - ③ - ④ - … - ②</p>
<p>说明：<br>1.do-while循环至少会执行一次循环体！<br>2.开发中，使用for和while更多一些。较少使用do-while</p>
<h3 id="关键字：beak-和-continue"><a href="#关键字：beak-和-continue" class="headerlink" title="关键字：beak 和 continue"></a>关键字：beak 和 continue</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">break和continue关键字的使用</span></span><br><span class="line"><span class="comment">				使用范围			循环中使用的作用(不同点)		相同点</span></span><br><span class="line"><span class="comment">break:			switch-case			</span></span><br><span class="line"><span class="comment">				循环结构中			结束当前循环					关键字后面不能声明执行语句	</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">continue:		循环结构中			结束当次循环					关键字后面不能声明执行语句</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">补充：带标签的break和continue的使用</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">BreakContinueTest</span> </span>&#123;</span><br><span class="line">*****************************</span><br><span class="line">		label:<span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>;i &lt;= <span class="number">4</span>;i++)&#123;</span><br><span class="line">			<span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">1</span>;j &lt;= <span class="number">10</span>;j++)&#123;</span><br><span class="line">				<span class="keyword">if</span>(j % <span class="number">4</span> == <span class="number">0</span>)&#123;</span><br><span class="line">					<span class="comment">//break;//默认跳出包裹此关键字最近的一层循环。</span></span><br><span class="line">					<span class="comment">//continue;</span></span><br><span class="line">					<span class="comment">//break label;//结束指定标识的一层循环结构    123</span></span><br><span class="line">					<span class="keyword">continue</span> label;<span class="comment">//结束指定标识的一层循环结构当次循环//输出   123123123123</span></span><br><span class="line">				&#125;</span><br><span class="line">				System.out.print(j);</span><br><span class="line">			&#125;</span><br><span class="line">			System.out.println();</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="质数的输出"><a href="#质数的输出" class="headerlink" title="质数的输出"></a>质数的输出</h3><h4 id="优化方式一"><a href="#优化方式一" class="headerlink" title="优化方式一"></a>优化方式一</h4><p>定义flag来判断是否为质数</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">*/</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">PrimeNumberTest1</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">boolean</span> isFlag = <span class="keyword">true</span>;<span class="comment">//标识i是否被j除尽，一旦除尽，修改其值</span></span><br><span class="line">		<span class="keyword">int</span> count = <span class="number">0</span>;<span class="comment">//记录质数的个数</span></span><br><span class="line"></span><br><span class="line">		<span class="comment">//获取当前时间距离1970-01-01 00:00:00 的毫秒数</span></span><br><span class="line">		<span class="keyword">long</span> start = System.currentTimeMillis();</span><br><span class="line"></span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">2</span>;i &lt;= <span class="number">100000</span>;i++)&#123;<span class="comment">//遍历100000以内的自然数</span></span><br><span class="line">			</span><br><span class="line">			<span class="comment">//优化二：对本身是质数的自然数是有效的。</span></span><br><span class="line">			<span class="comment">//for(int j = 2;j &lt; i;j++)&#123;</span></span><br><span class="line">			<span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">2</span>;j &lt;= Math.sqrt(i);j++)&#123;<span class="comment">//j:被i去除</span></span><br><span class="line">				</span><br><span class="line">				<span class="keyword">if</span>(i % j == <span class="number">0</span>)&#123; <span class="comment">//i被j除尽</span></span><br><span class="line">					isFlag = <span class="keyword">false</span>;</span><br><span class="line">					<span class="keyword">break</span>;<span class="comment">//优化一：只对本身非质数的自然数是有效的。</span></span><br><span class="line">				&#125;</span><br><span class="line">				</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="comment">//</span></span><br><span class="line">			<span class="keyword">if</span>(isFlag == <span class="keyword">true</span>)&#123;</span><br><span class="line">				<span class="comment">//System.out.println(i);</span></span><br><span class="line">				count++;</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="comment">//重置isFlag</span></span><br><span class="line">			isFlag = <span class="keyword">true</span>;</span><br><span class="line">		</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		<span class="comment">//获取当前时间距离1970-01-01 00:00:00 的毫秒数</span></span><br><span class="line">		<span class="keyword">long</span> end = System.currentTimeMillis();</span><br><span class="line">		System.out.println(<span class="string">"质数的个数为："</span> + count);</span><br><span class="line">		System.out.println(<span class="string">"所花费的时间为："</span> + (end - start));<span class="comment">//17110 - 优化一：break:1546 - 优化二：13</span></span><br><span class="line"></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h4 id="优化方式二"><a href="#优化方式二" class="headerlink" title="优化方式二"></a>优化方式二</h4><p>利用 continue label</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">PrimeNumberTest2</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">int</span> count = <span class="number">0</span>;<span class="comment">//记录质数的个数</span></span><br><span class="line"></span><br><span class="line">		<span class="comment">//获取当前时间距离1970-01-01 00:00:00 的毫秒数</span></span><br><span class="line">		<span class="keyword">long</span> start = System.currentTimeMillis();</span><br><span class="line"></span><br><span class="line">		label:<span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">2</span>;i &lt;= <span class="number">100000</span>;i++)&#123;<span class="comment">//遍历100000以内的自然数</span></span><br><span class="line">			</span><br><span class="line">			<span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">2</span>;j &lt;= Math.sqrt(i);j++)&#123;<span class="comment">//j:被i去除</span></span><br><span class="line">				</span><br><span class="line">				<span class="keyword">if</span>(i % j == <span class="number">0</span>)&#123; <span class="comment">//i被j除尽</span></span><br><span class="line">					<span class="keyword">continue</span> label;</span><br><span class="line">				&#125;</span><br><span class="line">				</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="comment">//能执行到此步骤的，都是质数</span></span><br><span class="line">			count++;</span><br><span class="line">		</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		<span class="comment">//获取当前时间距离1970-01-01 00:00:00 的毫秒数</span></span><br><span class="line">		<span class="keyword">long</span> end = System.currentTimeMillis();</span><br><span class="line">		System.out.println(<span class="string">"质数的个数为："</span> + count);</span><br><span class="line">		System.out.println(<span class="string">"所花费的时间为："</span> + (end - start));<span class="comment">//17110 - 优化一：break:1546 - 优化二：13</span></span><br><span class="line"></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="最大公因数与最小公倍数"><a href="#最大公因数与最小公倍数" class="headerlink" title="最大公因数与最小公倍数"></a>最大公因数与最小公倍数</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">题目：输入两个正整数m和n，求其最大公约数和最小公倍数。</span></span><br><span class="line"><span class="comment">比如：12和20的最大公约数是4，最小公倍数是60。</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">说明：break关键字的使用：一旦在循环中执行到break，就跳出循环</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ForTest</span></span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span>&#123;</span><br><span class="line">	</span><br><span class="line">		Scanner scan = <span class="keyword">new</span> Scanner(System.in);</span><br><span class="line"></span><br><span class="line">		System.out.println(<span class="string">"请输入第一个正整数："</span>);</span><br><span class="line">		<span class="keyword">int</span> m = scan.nextInt();</span><br><span class="line">		</span><br><span class="line">		System.out.println(<span class="string">"请输入第二个正整数："</span>);</span><br><span class="line">		<span class="keyword">int</span> n = scan.nextInt();</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//获取最大公约数</span></span><br><span class="line">		<span class="comment">//1.获取两个数中的较小值</span></span><br><span class="line">		<span class="keyword">int</span> min = (m &lt;= n)? m : n;</span><br><span class="line">		<span class="comment">//2.遍历</span></span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> i = min;i &gt;= <span class="number">1</span> ;i--)&#123;</span><br><span class="line">			<span class="keyword">if</span>(m % i == <span class="number">0</span> &amp;&amp; n % i == <span class="number">0</span>)&#123;</span><br><span class="line">				System.out.println(<span class="string">"最大公约数为："</span> + i);</span><br><span class="line">				<span class="keyword">break</span>;<span class="comment">//一旦在循环中执行到break，就跳出循环</span></span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//获取最小公倍数</span></span><br><span class="line">		<span class="comment">//1.获取两个数中的较大值</span></span><br><span class="line">		<span class="keyword">int</span> max = (m &gt;= n)? m : n;</span><br><span class="line">		<span class="comment">//2.遍历</span></span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> i = max;i &lt;= m * n;i++)&#123;</span><br><span class="line">			<span class="keyword">if</span>(i % m == <span class="number">0</span> &amp;&amp; i % n == <span class="number">0</span>)&#123;</span><br><span class="line">				</span><br><span class="line">				System.out.println(<span class="string">"最小公倍数："</span> + i);</span><br><span class="line">				<span class="keyword">break</span>;</span><br><span class="line">			</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="附加-特殊流程控制语句3"><a href="#附加-特殊流程控制语句3" class="headerlink" title="附加:特殊流程控制语句3"></a>附加:特殊流程控制语句3</h2><ul>
<li><strong>return</strong>:并非专门用于结束循环的，它的功能是<strong>结束一个方法</strong>。 当一个方法执行到一个return语句时，这个方法将被结束。</li>
<li>与break和continue不同的是，return直接结束整个方法，不管这个return处于多少层循环之内</li>
</ul>
<h2 id="Scanner-中next-与-nextLine-区别"><a href="#Scanner-中next-与-nextLine-区别" class="headerlink" title="Scanner 中next() 与 nextLine() 区别"></a>Scanner 中next() 与 nextLine() 区别</h2><p>next():</p>
<ul>
<li>1、一定要读取到有效字符后才可以结束输入。</li>
<li>2、对输入有效字符之前遇到的空白，next() 方法会自动将其去掉。</li>
<li>3、只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符。 </li>
<li><strong>next() 不能得到带有空格的字符串</strong>。</li>
</ul>
<p>nextLine()： </p>
<ul>
<li>1、<strong>nextLine() 以Enter为结束符,也就是说 nextLine()方法返回的是输入回车之前的所有字符。</strong> </li>
<li>2、<strong>可以获得空白</strong>。</li>
</ul>
<p>如果要输入 int 或 float 类型的数据，在 Scanner 类中也有支持，但是在输入之前最好先使用 hasNextXxx() 方法进行验证，再使用 nextXxx() 来读取:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">static</span> String <span class="title">readKeyBoard</span><span class="params">(<span class="keyword">int</span> limit)</span> </span>&#123;</span><br><span class="line">        String line = <span class="string">""</span>;</span><br><span class="line">        <span class="keyword">while</span> (scanner.hasNext()) &#123;</span><br><span class="line">            line = scanner.nextLine();</span><br><span class="line">            <span class="keyword">if</span> (line.length() &lt; <span class="number">1</span> || line.length() &gt; limit) &#123;</span><br><span class="line">                System.out.print(<span class="string">"输入长度（不大于"</span> + limit + <span class="string">"）错误，请重新输入："</span>);</span><br><span class="line">                <span class="keyword">continue</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> line;</span><br><span class="line">    &#125;<span class="comment">//记录并且判断输入字符的长度是否符合要求</span></span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ScannerDemo</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Scanner scan = <span class="keyword">new</span> Scanner(System.in);</span><br><span class="line">        <span class="comment">// 从键盘接收数据</span></span><br><span class="line">        <span class="keyword">int</span> i = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">float</span> f = <span class="number">0.0f</span>;</span><br><span class="line">        System.out.print(<span class="string">"输入整数："</span>);</span><br><span class="line">        <span class="keyword">if</span> (scan.hasNextInt()) &#123;</span><br><span class="line">            <span class="comment">// 判断输入的是否是整数</span></span><br><span class="line">            i = scan.nextInt();</span><br><span class="line">            <span class="comment">// 接收整数</span></span><br><span class="line">            System.out.println(<span class="string">"整数数据："</span> + i);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="comment">// 输入错误的信息</span></span><br><span class="line">            System.out.println(<span class="string">"输入的不是整数！"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.print(<span class="string">"输入小数："</span>);</span><br><span class="line">        <span class="keyword">if</span> (scan.hasNextFloat()) &#123;</span><br><span class="line">            <span class="comment">// 判断输入的是否是小数</span></span><br><span class="line">            f = scan.nextFloat();</span><br><span class="line">            <span class="comment">// 接收小数</span></span><br><span class="line">            System.out.println(<span class="string">"小数数据："</span> + f);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="comment">// 输入错误的信息</span></span><br><span class="line">            System.out.println(<span class="string">"输入的不是小数！"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        scan.close();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">输出结果为：</span></span><br><span class="line"><span class="comment">$ javac ScannerDemo.java</span></span><br><span class="line"><span class="comment">$ java ScannerDemo</span></span><br><span class="line"><span class="comment">输入整数：12</span></span><br><span class="line"><span class="comment">整数数据：12</span></span><br><span class="line"><span class="comment">输入小数：1.2</span></span><br><span class="line"><span class="comment">小数数据：1.2</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>

<h2 id="衡量一个功能代码的优劣"><a href="#衡量一个功能代码的优劣" class="headerlink" title="衡量一个功能代码的优劣"></a>衡量一个功能代码的优劣</h2><ol>
<li>正确性</li>
<li>可读性</li>
<li>健壮性</li>
<li>高效率与低存储：<strong>时间复杂度</strong>、空间复杂度（衡量算法的好坏）</li>
</ol>
<h2 id="数组"><a href="#数组" class="headerlink" title="数组"></a>数组</h2><ol>
<li>数组是多个相同类型数据按一定顺序排列的集合，并使用一个名字命名，并通过编号的方式对这些数据进行统一的管理</li>
<li>数组相关的概念：<ol>
<li>数组名</li>
<li>元素</li>
<li>角标、下标、索引</li>
<li>数组的长度：元素个数</li>
</ol>
</li>
<li>数组的特点：<ol>
<li>数组是序排序的</li>
<li>数组属于引用数据类型的变量。数组的元素，既可以是基本数据类型，也可以是引用数据类型</li>
<li>创建数组对象会在内存中开辟一整块连续的空间</li>
<li>数组的长度一旦确定，就不能修改</li>
</ol>
</li>
<li>数组的分类：<ol>
<li>按照维数：一维数组、二维数组、、、、、、</li>
<li>按照数组元素的类型：基本数据类型元素的数组、引用数据元素的数组</li>
</ol>
</li>
</ol>
<ul>
<li><p>写出一维数组初始化的两种方式</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span>[] arr = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">5</span>]; <span class="comment">//动态初始化</span></span><br><span class="line">String[] arr1 = <span class="keyword">new</span> String[]&#123;<span class="string">"Tom"</span>,<span class="string">"Jerry"</span>,<span class="string">"Jim"</span>&#125;;<span class="comment">//静态初始化</span></span><br><span class="line">数组一初始化，其长度就是确定的。arr.length</span><br><span class="line">数组长度一旦确定，就不可以修改</span><br></pre></td></tr></table></figure>
</li>
<li><p>写出二维数组初始化的两种方式</p>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span>[][] arr = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">4</span>][<span class="number">3</span>];<span class="comment">//动态初始化1</span></span><br><span class="line"><span class="keyword">int</span>[][] arr1 = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">4</span>][];<span class="comment">//动态初始化2</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span>[][] arr2 = <span class="keyword">new</span> <span class="keyword">int</span>[][]&#123;&#123;<span class="number">1</span>,<span class="number">2</span>&#125;,&#123;<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>&#125;&#125;;</span><br></pre></td></tr></table></figure>

<ul>
<li>如何遍历如下数组</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span>[] arr  = <span class="keyword">new</span> <span class="keyword">int</span> [][]&#123;&#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>&#125;,&#123;<span class="number">4</span>,<span class="number">5</span>&#125;,&#123;<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>&#125;&#125;;</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>;i &lt; arr.length;i++)&#123;</span><br><span class="line">  <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">0</span>;j &lt; arr[i].length;j++)&#123;</span><br><span class="line">    System.out.print(arr[i][j] + <span class="string">"\t"</span>);</span><br><span class="line">  &#125;</span><br><span class="line">  System.out.println();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>不同类型的一维数组的默认初始化值各是多少</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">整型: <span class="number">0</span></span><br><span class="line">浮点型：<span class="number">0.0</span></span><br><span class="line"><span class="keyword">char</span>：<span class="number">0</span></span><br><span class="line"><span class="keyword">boolean</span>:<span class="keyword">false</span></span><br><span class="line">引用类型：<span class="keyword">null</span></span><br></pre></td></tr></table></figure>

<ul>
<li>一维数组的内存解析</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">String[] strs = <span class="keyword">new</span> String[<span class="number">5</span>];</span><br><span class="line">strs[<span class="number">2</span>] = <span class="string">"Tom"</span>;</span><br><span class="line">strs = <span class="keyword">new</span> String[<span class="number">3</span>];</span><br></pre></td></tr></table></figure>

<p>String[] strs = new String[5];</p>
<p>![image-20200216234054034](/Users/yangshucheng/Library/Application Support/typora-user-images/image-20200216234054034.png)</p>
<p>strs[2] = “Tom”;</p>
<p>![image-20200216234155075](/Users/yangshucheng/Library/Application Support/typora-user-images/image-20200216234155075.png)</p>
<p>strs = new String[3];</p>
<p>![image-20200216234427490](/Users/yangshucheng/Library/Application Support/typora-user-images/image-20200216234427490.png)</p>
<p>sysout[strs[2]];//null</p>
<p>sysout[strs[4]];//报错</p>
<p>真实情况下，String中的数据放在常量池中，数组都是地址值</p>
<p>![image-20200216234738387](/Users/yangshucheng/Library/Application Support/typora-user-images/image-20200216234738387.png)</p>
<h3 id="一维数组的声明和初始化"><a href="#一维数组的声明和初始化" class="headerlink" title="一维数组的声明和初始化"></a>一维数组的声明和初始化</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">  <span class="comment">//1. 一维数组的声明和初始化</span></span><br><span class="line"><span class="keyword">int</span> num;<span class="comment">//声明</span></span><br><span class="line">num = <span class="number">10</span>;<span class="comment">//初始化</span></span><br><span class="line"><span class="keyword">int</span> id = <span class="number">1001</span>;<span class="comment">//声明 + 初始化</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span>[] ids;<span class="comment">//声明</span></span><br><span class="line"><span class="comment">//1.1 静态初始化:数组的初始化和数组元素的赋值操作同时进行</span></span><br><span class="line">ids = <span class="keyword">new</span> <span class="keyword">int</span>[]&#123;<span class="number">1001</span>,<span class="number">1002</span>,<span class="number">1003</span>,<span class="number">1004</span>&#125;;</span><br><span class="line"><span class="comment">//1.2动态初始化:数组的初始化和数组元素的赋值操作分开进行</span></span><br><span class="line">String[] names = <span class="keyword">new</span> String[<span class="number">5</span>];</span><br><span class="line"></span><br><span class="line">  <span class="comment">//也是正确的写法：</span></span><br><span class="line"><span class="keyword">int</span>[] arr4 = &#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>&#125;;<span class="comment">//类型推断</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//总结：数组一旦初始化完成，其长度就确定了。</span></span><br></pre></td></tr></table></figure>

<h3 id="如何调用数组的指定位置的元素"><a href="#如何调用数组的指定位置的元素" class="headerlink" title="如何调用数组的指定位置的元素"></a>如何调用数组的指定位置的元素</h3><p>通过角标的方式调用。</p>
<p>数组的角标（或索引）从0开始的，到数组的长度-1结束。</p>
<h3 id="如何获取数组的长度。"><a href="#如何获取数组的长度。" class="headerlink" title="如何获取数组的长度。"></a>如何获取数组的长度。</h3><p>属性:length</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">System.out.println(names.length);</span><br><span class="line">System.out.println(ids.length);</span><br></pre></td></tr></table></figure>

<h3 id="如何遍历数组"><a href="#如何遍历数组" class="headerlink" title="如何遍历数组"></a>如何遍历数组</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>;i &lt; names.length;i++)&#123;</span><br><span class="line">			System.out.println(names[i]);</span><br><span class="line">		&#125;</span><br></pre></td></tr></table></figure>

<h3 id="数组元素的默认初始化值"><a href="#数组元素的默认初始化值" class="headerlink" title="数组元素的默认初始化值"></a>数组元素的默认初始化值</h3><ul>
<li><p>数组元素是整型：0</p>
</li>
<li><p>数组元素是浮点型：0.0</p>
</li>
<li><p>数组元素是char型：0或’\u0000’，而非’0’</p>
</li>
<li><p>数组元素是boolean型：false</p>
</li>
<li><p>数组元素是引用数据类型：null</p>
</li>
</ul>
<h2 id="二维数组的使用"><a href="#二维数组的使用" class="headerlink" title="二维数组的使用"></a>二维数组的使用</h2><h4 id="理解"><a href="#理解" class="headerlink" title="理解"></a>理解</h4><ul>
<li>对于二维数组的理解，我们可以看成是一维数组array1又作为另一个一维数组array2的元素而存在。其实，从数组底层的运行机制来看，其实没有多维数组</li>
</ul>
<h4 id="使用"><a href="#使用" class="headerlink" title="使用:"></a>使用:</h4><p> *  ① 二维数组的声明和初始化</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//二维数组的声明和初始化</span></span><br><span class="line"><span class="keyword">int</span>[] arr = <span class="keyword">new</span> <span class="keyword">int</span>[]&#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>&#125;;<span class="comment">//一维数组</span></span><br><span class="line"><span class="comment">//静态初始化</span></span><br><span class="line"><span class="keyword">int</span>[][] arr1 = <span class="keyword">new</span> <span class="keyword">int</span>[][]&#123;&#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>&#125;,&#123;<span class="number">4</span>,<span class="number">5</span>&#125;,&#123;<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>&#125;&#125;;</span><br><span class="line"><span class="comment">//动态初始化1</span></span><br><span class="line">String[][] arr2 = <span class="keyword">new</span> String[<span class="number">3</span>][<span class="number">2</span>];</span><br><span class="line"><span class="comment">//动态初始化2</span></span><br><span class="line">String[][] arr3 = <span class="keyword">new</span> String[<span class="number">3</span>][];</span><br><span class="line"><span class="comment">//也是正确的写法：</span></span><br><span class="line"><span class="keyword">int</span>[] arr4[] = <span class="keyword">new</span> <span class="keyword">int</span>[][]&#123;&#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>&#125;,&#123;<span class="number">4</span>,<span class="number">5</span>,<span class="number">9</span>,<span class="number">10</span>&#125;,&#123;<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>&#125;&#125;;</span><br><span class="line"><span class="keyword">int</span>[] arr5[] = &#123;&#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>&#125;,&#123;<span class="number">4</span>,<span class="number">5</span>&#125;,&#123;<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>&#125;&#125;;</span><br></pre></td></tr></table></figure>



<p> *  ② 如何调用数组的指定位置的元素</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">System.out.println(arr1[<span class="number">0</span>][<span class="number">1</span>]);<span class="comment">//2</span></span><br><span class="line">System.out.println(arr2[<span class="number">1</span>][<span class="number">1</span>]);<span class="comment">//null</span></span><br><span class="line"></span><br><span class="line">arr3[<span class="number">1</span>] = <span class="keyword">new</span> String[<span class="number">4</span>];</span><br><span class="line">System.out.println(arr3[<span class="number">1</span>][<span class="number">0</span>]);</span><br></pre></td></tr></table></figure>



<p> *  ③ 如何获取数组的长度</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">System.out.println(arr4.length);<span class="comment">//3</span></span><br><span class="line">System.out.println(arr4[<span class="number">0</span>].length);<span class="comment">//3</span></span><br><span class="line">System.out.println(arr4[<span class="number">1</span>].length);<span class="comment">//4</span></span><br></pre></td></tr></table></figure>

<p> *  ④ 如何遍历数组</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>;i &lt; arr4.length;i++)&#123;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">0</span>;j &lt; arr4[i].length;j++)&#123;</span><br><span class="line">		System.out.print(arr4[i][j] + <span class="string">"  "</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	System.out.println();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p> *  ⑤ 数组元素的默认初始化值 </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">规定：二维数组分为外层数组的元素，内层数组的元素</span><br><span class="line"> * 		<span class="keyword">int</span>[][] arr = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">4</span>][<span class="number">3</span>];</span><br><span class="line"> * 		外层元素：arr[<span class="number">0</span>],arr[<span class="number">1</span>]等</span><br><span class="line"> * 		内层元素：arr[<span class="number">0</span>][<span class="number">0</span>],arr[<span class="number">1</span>][<span class="number">2</span>]等</span><br><span class="line"> * </span><br><span class="line"> *   ⑤ 数组元素的默认初始化值 </span><br><span class="line"> *   针对于初始化方式一：比如：<span class="keyword">int</span>[][] arr = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">4</span>][<span class="number">3</span>];</span><br><span class="line"> *      外层元素的初始化值为：地址值</span><br><span class="line"> *      内层元素的初始化值为：与一维数组初始化情况相同</span><br><span class="line"> *      </span><br><span class="line"> *   针对于初始化方式二：比如：<span class="keyword">int</span>[][] arr = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">4</span>][];</span><br><span class="line"> *   	外层元素的初始化值为：<span class="keyword">null</span></span><br><span class="line"> *      内层元素的初始化值为：不能调用，否则报错。</span><br></pre></td></tr></table></figure>



<p> *  ⑥ 数组的内存解析 </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">		System.out.println(<span class="string">"*****************"</span>);</span><br><span class="line">		<span class="keyword">double</span>[][] arr3 = <span class="keyword">new</span> <span class="keyword">double</span>[<span class="number">4</span>][];</span><br><span class="line">		System.out.println(arr3[<span class="number">1</span>]);<span class="comment">//null</span></span><br><span class="line"><span class="comment">//		System.out.println(arr3[1][0]);//报错</span></span><br></pre></td></tr></table></figure>


      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blog/tags/Eclipse/" rel="tag"># Eclipse</a>
          
            <a href="/blog/tags/Java%E5%9F%BA%E7%A1%80/" rel="tag"># Java基础</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2020/03/27/Eclipse%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9C/" rel="next" title="Eclipse基本操作">
                <i class="fa fa-chevron-left"></i> Eclipse基本操作
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">羊书</p>
              <p class="site-description motion-element" itemprop="description">记录生活</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/%7C%7Carchive">
              
                  <span class="site-state-item-count">8</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/blog/categories/index.html">
                  <span class="site-state-item-count">1</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/blog/tags/index.html">
                  <span class="site-state-item-count">16</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/yangshucheng1219" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:ysc714@163.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#JRE、JDL、JVM的区别与联系"><span class="nav-number">1.</span> <span class="nav-text">JRE、JDL、JVM的区别与联系</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#java程序编写-编译-运行的过程"><span class="nav-number">2.</span> <span class="nav-text">java程序编写-编译-运行的过程</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#java-程序注意"><span class="nav-number">3.</span> <span class="nav-text">java 程序注意</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#java数据类型"><span class="nav-number">4.</span> <span class="nav-text">java数据类型</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#输出语句"><span class="nav-number">5.</span> <span class="nav-text">输出语句</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#输出数组"><span class="nav-number">6.</span> <span class="nav-text">输出数组</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#注释"><span class="nav-number">7.</span> <span class="nav-text">注释</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#单行注释"><span class="nav-number">7.1.</span> <span class="nav-text">单行注释</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#多行注释"><span class="nav-number">7.2.</span> <span class="nav-text">多行注释</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#文档注释"><span class="nav-number">7.3.</span> <span class="nav-text">文档注释</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Java-关键字"><span class="nav-number">8.</span> <span class="nav-text">Java 关键字</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Java保留字"><span class="nav-number">9.</span> <span class="nav-text">Java保留字</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#标识符的使用"><span class="nav-number">10.</span> <span class="nav-text">标识符的使用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Java中的名称命名规范"><span class="nav-number">11.</span> <span class="nav-text">Java中的名称命名规范</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Java变量的分类"><span class="nav-number">12.</span> <span class="nav-text">Java变量的分类</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#按数据类型分"><span class="nav-number">12.1.</span> <span class="nav-text">按数据类型分</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#基本数据类型"><span class="nav-number">12.1.1.</span> <span class="nav-text">基本数据类型</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#引用数据类型"><span class="nav-number">12.1.2.</span> <span class="nav-text">引用数据类型</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#详细说明"><span class="nav-number">12.1.3.</span> <span class="nav-text">详细说明</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#按变量的分类-按声明的位置的不同分类"><span class="nav-number">12.2.</span> <span class="nav-text">按变量的分类-按声明的位置的不同分类</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#变量声明"><span class="nav-number">13.</span> <span class="nav-text">变量声明</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#使用变量注意"><span class="nav-number">13.1.</span> <span class="nav-text">使用变量注意</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#基本数据类型变量间运算规则"><span class="nav-number">14.</span> <span class="nav-text">基本数据类型变量间运算规则</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#String与8种基本数据类型间的运算"><span class="nav-number">15.</span> <span class="nav-text">String与8种基本数据类型间的运算</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#二进制"><span class="nav-number">16.</span> <span class="nav-text">二进制</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#算数运算符"><span class="nav-number">17.</span> <span class="nav-text">算数运算符</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#运算符之一：算术运算符"><span class="nav-number">17.1.</span> <span class="nav-text">运算符之一：算术运算符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#运算符之二：赋值运算符"><span class="nav-number">17.2.</span> <span class="nav-text">运算符之二：赋值运算符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#运算符之三：比较运算符"><span class="nav-number">17.3.</span> <span class="nav-text">运算符之三：比较运算符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#运算符之四：逻辑运算符"><span class="nav-number">17.4.</span> <span class="nav-text">运算符之四：逻辑运算符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#运算符之四：位运算符"><span class="nav-number">17.5.</span> <span class="nav-text">运算符之四：位运算符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#交换两个变量的办法"><span class="nav-number">17.6.</span> <span class="nav-text">交换两个变量的办法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#如何求一个0-255范围内的整数的十六进制值，例如60的十六进制表示形式3C"><span class="nav-number">17.7.</span> <span class="nav-text">如何求一个0~255范围内的整数的十六进制值，例如60的十六进制表示形式3C</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#运算符之四：三元运算符"><span class="nav-number">17.8.</span> <span class="nav-text">运算符之四：三元运算符</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#流程控制"><span class="nav-number">18.</span> <span class="nav-text">流程控制</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#分支结构中的if-else（条件判断结构）"><span class="nav-number">18.1.</span> <span class="nav-text">分支结构中的if-else（条件判断结构）</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#switch-case结构"><span class="nav-number">18.2.</span> <span class="nav-text">switch-case结构</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#循环结构"><span class="nav-number">18.3.</span> <span class="nav-text">循环结构</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#for"><span class="nav-number">18.3.1.</span> <span class="nav-text">for</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#while"><span class="nav-number">18.3.2.</span> <span class="nav-text">while</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#do-while"><span class="nav-number">18.3.3.</span> <span class="nav-text">do-while</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#关键字：beak-和-continue"><span class="nav-number">18.4.</span> <span class="nav-text">关键字：beak 和 continue</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#质数的输出"><span class="nav-number">18.5.</span> <span class="nav-text">质数的输出</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#优化方式一"><span class="nav-number">18.5.1.</span> <span class="nav-text">优化方式一</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#优化方式二"><span class="nav-number">18.5.2.</span> <span class="nav-text">优化方式二</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#最大公因数与最小公倍数"><span class="nav-number">18.6.</span> <span class="nav-text">最大公因数与最小公倍数</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#附加-特殊流程控制语句3"><span class="nav-number">19.</span> <span class="nav-text">附加:特殊流程控制语句3</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Scanner-中next-与-nextLine-区别"><span class="nav-number">20.</span> <span class="nav-text">Scanner 中next() 与 nextLine() 区别</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#衡量一个功能代码的优劣"><span class="nav-number">21.</span> <span class="nav-text">衡量一个功能代码的优劣</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#数组"><span class="nav-number">22.</span> <span class="nav-text">数组</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#一维数组的声明和初始化"><span class="nav-number">22.1.</span> <span class="nav-text">一维数组的声明和初始化</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#如何调用数组的指定位置的元素"><span class="nav-number">22.2.</span> <span class="nav-text">如何调用数组的指定位置的元素</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#如何获取数组的长度。"><span class="nav-number">22.3.</span> <span class="nav-text">如何获取数组的长度。</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#如何遍历数组"><span class="nav-number">22.4.</span> <span class="nav-text">如何遍历数组</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#数组元素的默认初始化值"><span class="nav-number">22.5.</span> <span class="nav-text">数组元素的默认初始化值</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#二维数组的使用"><span class="nav-number">23.</span> <span class="nav-text">二维数组的使用</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#理解"><span class="nav-number">23.0.1.</span> <span class="nav-text">理解</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#使用"><span class="nav-number">23.0.2.</span> <span class="nav-text">使用:</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">羊书</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
